﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
    public class FileOperate
    {
        string _DriveConnFileName = "DriveConnectionMeaage.xml.";
        string _WorkMessageFileName = "WorkMessage.xml.";
        string _PLCWorkAddressFileName = "PLCWorkAddress.xml.";
        string _AGVGoodsNOFileName = "AGVGoodsNO.xml.";
        /// <summary>
        /// 路径处理
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public string FilePathProcess(string FileName)
        {
            string FilePath = AppDomain.CurrentDomain.BaseDirectory + "\\Config" + "\\" + FileName;
            return FilePath;
        }
        /// <summary>
        /// 读取设备连接列表
        /// </summary>
        /// <returns></returns>
        public List<DeviceConnMessage> ReadDriveConnMessage()
        {
            string DeviceConnFilePath = FilePathProcess(_DriveConnFileName);
            List<DeviceConnMessage> DeviceConnMessageList = (List<DeviceConnMessage>)Serializition.LoadFromFile(typeof(List<DeviceConnMessage>), DeviceConnFilePath);
            return DeviceConnMessageList;
        }
        /// <summary>
        /// 写入设备连接列表
        /// </summary>
        /// <param name="DeviceConnMessageList"></param>
        /// <returns></returns>
        public bool WriteDriveConnMessage(List<DeviceConnMessage> DeviceConnMessageList)
        {
            try
            {
                string DeviceConnFilePath = FilePathProcess(_DriveConnFileName);
                Serializition.SaveToFile(DeviceConnMessageList, DeviceConnFilePath);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 读取工作信息
        /// </summary>
        /// <returns></returns>
        public WorkMessage ReadWorkMssage()
        {
            string WorkMessageFilePath = FilePathProcess(_WorkMessageFileName);
            WorkMessage LocalityWorkMessage = (WorkMessage)Serializition.LoadFromFile(typeof(WorkMessage), WorkMessageFilePath);
            return LocalityWorkMessage;
        }
        /// <summary>
        /// 写入工作信息
        /// </summary>
        /// <param name="WorkingMessage"></param>
        /// <returns></returns>
        public bool WriteWorkMssage(WorkMessage WorkingMessage)
        {
            try
            {
                string WorkMessageFilePath = FilePathProcess(_WorkMessageFileName);
                Serializition.SaveToFile(WorkingMessage, WorkMessageFilePath);
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
        /// <summary>
        /// 写入PLC地址信息
        /// </summary>
        /// <param name="PLCAddress"></param>
        /// <returns></returns>
        public bool WritePLCAddress(PLCWorkAddress PLCAddress)
        {
            try
            {
                string PLCAddressFilePath = FilePathProcess(_PLCWorkAddressFileName);
                Serializition.SaveToFile(PLCAddress, PLCAddressFilePath);
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
        /// <summary>
        /// 读取PLC地址信息
        /// </summary>
        /// <returns></returns>
        public PLCWorkAddress ReadPLCAddress()
        {
            string PLCAddressFilePath = FilePathProcess(_PLCWorkAddressFileName);
            PLCWorkAddress PLCAddressMessage = (PLCWorkAddress)Serializition.LoadFromFile(typeof(PLCWorkAddress), PLCAddressFilePath);
            return PLCAddressMessage;
        }

        /// <summary>
        /// 读取AGV物料编号
        /// </summary>
        /// <returns></returns>
        public AGVNO ReadAGVGoodsMessage()
        {
            string AGVGoodsNOPath = FilePathProcess(_AGVGoodsNOFileName);
            AGVNO AGVGoodsNOMessage= (AGVNO)Serializition.LoadFromFile(typeof(AGVNO), AGVGoodsNOPath);
            return AGVGoodsNOMessage;
        }

        /// <summary>
        /// 写入AGV物料编号
        /// </summary>
        /// <param name="aGVGoodsNO"></param>
        /// <returns></returns>
        public bool WriteAGVGoodsMessage(AGVNO aGVGoodsNO)
        {
            try
            {
                string AGVGoodsNOPath = FilePathProcess(_AGVGoodsNOFileName);
                Serializition.SaveToFile(aGVGoodsNO, AGVGoodsNOPath);
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
    }
}
